class TrackController {
    def authenticateService

    def line = {
          return [];
    }
    
    def track = {
        if (!params.id) {
            flash.message = "Device id not provided"
            redirect(controller: "device", action: "list")
        } else {
            def device = Device.get(params.id)
            if (!device) {
                flash.message = "Device not found with id ${params.id}"
                redirect(controller: "device", action: "list")
            } else {
                if (authenticateService.ifAllGranted("ROLE_ADMIN")) {
                    return [device: device]
                } else if (authenticateService.ifAllGranted("ROLE_ADMIN")) {
                    if (device.devicegroup.user.id == authenticateService.userDomain().id) {
                        return [device: device]
                    } else {
                        flash.message = "Device not found with id ${params.id}"
                        redirect(controller: "device", action: "list")
                    }
                } else {
                    flash.message = "Device not found with id ${params.id}"
                    redirect(controller: "device", action: "list")
                }
            }
        }
    }

    def trackgroup = {
        println "track"
        println params
        if (!params.id) {
            flash.message = "Devicegroup id not provided"
            redirect(controller: "devicegroup", action: "list")
        } else {
            def devicegroup = Devicegroup.get(params.id)
            if (!devicegroup) {
                flash.message = "devicegroup not found with id ${params.id}"
                redirect(controller: "devicegroup", action: "list")
            } else {
                if (authenticateService.ifAllGranted("ROLE_ADMIN")) {
                    return [devicegroup: devicegroup]
                } else if (authenticateService.ifAllGranted("ROLE_ADMIN")) {
                    if (devicegroup.user.id == authenticateService.userDomain().id) {
                        return [devicegroup: devicegroup]
                    } else {
                        flash.message = "devicegroup not found with id ${params.id}"
                        redirect(controller: "devicegroup", action: "list")
                    }
                } else {
                    flash.message = "devicegroup not found with id ${params.id}"
                    redirect(controller: "devicegroup", action: "list")
                }
            }
        }
    }
}
